package top.aylan.org.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import top.aylan.org.dao.ProjectMapper;
import top.aylan.org.entity.Blog;
import top.aylan.org.entity.Project;
import top.aylan.org.entity.ProjectExample;
import top.aylan.org.service.ProjectService;
import top.aylan.org.tools.Pager;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2018/12/29.
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Resource
    ProjectMapper projectMapper;

    @Override
    public List<Project> selectProjectByPage(Pager pager) throws Exception {

        ProjectExample example = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        example.setOfset((pager.getCurrentPage() - 1)*pager.getEveyPageRow());
        example.setSize(pager.getEveyPageRow());
        return projectMapper.selectByExampleWithPage(example);
    }

    @Override
    public int getCount(ProjectExample example) throws Exception {
        ProjectExample example1 = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        return projectMapper.countByExample(example);
    }

    @Override
    public Project findById(Integer id) throws Exception {
        return projectMapper.selectByPrimaryKey(id);
    }

    @Override
    public Project findByExample(ProjectExample example) throws Exception {
        ProjectExample example1 = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        return projectMapper.selectByExample(example).get(0);
    }

    @Override
    public int delete(Integer id) throws Exception {
        return projectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int save(Project Project) throws Exception {
        return projectMapper.insert(Project);
    }

    @Override
    public int updateById(Integer id, Project project) throws Exception {
        project.setId(id);
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public List<Blog> selectBlogsByPage(Pager<Project> pager) {

        ProjectExample example = new ProjectExample();
        ProjectExample.Criteria criteria = example.createCriteria();
        Project project = (Project) pager.getCondition();
        if(project.getCategoryId()!=null && project.getCategoryId() > 0){
            criteria.andCategoryIdEqualTo(project.getCategoryId());
        }
        pager.setTotalRow(projectMapper.countByExample(example));
        example.setOfset((pager.getCurrentPage() - 1)*pager.getEveyPageRow());
        example.setSize(pager.getEveyPageRow());
        return projectMapper.selectBlogsByPage(example);
    }
}
